//
//  ConfigManager.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/3/12.
//

import Foundation

//ai人设管理
class AiDescribeManager : BaseViewModel {
    static let shared = AiDescribeManager()

    private let userDefaults = UserDefaults.standard
    private let userKey = "aiDescribeKey"
    @Published var config : AiDescribeConfig? {
        didSet {
            saveConfig()
        }
    }
    
    //保存本地数据
    private func saveConfig() {
        if let config = config {
            let encoder = JSONEncoder()
            if let encodedData = try? encoder.encode(config) {
                userDefaults.set(encodedData, forKey: userKey)
            }
        } else {
            userDefaults.removeObject(forKey: userKey)
        }
    }
    
    //加载本地数据
    private func loadConfig() -> AiDescribeConfig? {
        if let encodedData = userDefaults.data(forKey: userKey),
           let decodedUser = try? JSONDecoder().decode(AiDescribeConfig.self, from: encodedData) {
            return decodedUser
        }
        return nil
    }
    
    
    func getConfig() -> AiDescribeConfig {
        return config ?? AiDescribeConfig()
    }
}
